package meijuli.com.join51.business.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.view.inputmethod.InputMethodManager;

import java.util.List;

import meijuli.com.join51.business.R;
import com.jhx.common.widget.CommonDialog;
import com.jhx.common.widget.LoadingDialog;
import meijuli.com.join51.business.utils.PermissionUtils;
import pub.devrel.easypermissions.EasyPermissions;

/**
 * 基类
 *
 * @author wenqin 2017-04-13 15:37
 */

public class BaseFragment extends Fragment {
    private FragmentTransaction mTransaction;
    private LoadingDialog mLoadingDialog;
    private CommonDialog mDialog = null;
    protected FragmentActivity mActivity;

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mActivity = getActivity();
    }

    protected void switchFragment(Fragment fromFragment, Fragment toFragment) {
        if(mActivity == null && mActivity.isFinishing()) {
            return;
        }
        mTransaction = mActivity.getSupportFragmentManager().beginTransaction()
                .setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out,
                        android.R.anim.slide_in_left, android.R.anim.slide_out_right)
                .addToBackStack(null);
        if (!toFragment.isAdded()) {
            mTransaction.hide(fromFragment)
                    .add(R.id.content_view, toFragment)
                    .commit();
        } else {
            mTransaction.hide(fromFragment)
                    .show(toFragment)
                    .commit();
        }
    }

    /**
     * fragment出栈
     */
    protected void pop() {
        if(mActivity == null && mActivity.isFinishing()) {
            return;
        }
        FragmentManager fm = mActivity.getSupportFragmentManager();
        if (fm.getBackStackEntryCount() > 0) {
            fm.popBackStack();
            return;
        }
        mActivity.finish();
    }

    protected void hideKeyboard(View view) {
        if (view != null) {
            InputMethodManager methodManager = (InputMethodManager) getContext()
                    .getSystemService(Context.INPUT_METHOD_SERVICE);
            methodManager.hideSoftInputFromInputMethod(view.getWindowToken(), 0);
        }
    }

    protected void checkPermission(List<String> permissionList, String message) {
        if (EasyPermissions.somePermissionPermanentlyDenied(this, permissionList)) {
            if (mDialog == null) {
                mDialog = new CommonDialog(getActivity());
            }
            mDialog.setMessage(message);
            mDialog.setCanceledOnTouchOutside(false);
            if (!mDialog.isShowing()) {
                mDialog.show();
            }
            mDialog.setOnClickListener(new CommonDialog.OnClickListener() {
                @Override
                public void onConfirmClick(View view) {
                    PermissionUtils.getInstance().toSetting(getActivity());
                    mDialog.dismiss();
                }

                @Override
                public void onCancelClick(View view) {
                    mDialog.dismiss();
                }
            });
            mDialog.setCancelable(false);
        }
    }

    protected void showLoadingDialog() {
        if (mLoadingDialog == null) {
            mLoadingDialog = new LoadingDialog(getActivity());
        }
        mLoadingDialog.dismiss();
        mLoadingDialog.show();
    }

    protected void showLoadingDialog(String message) {
        if (mLoadingDialog == null) {
            mLoadingDialog = new LoadingDialog(getActivity());
        }
        mLoadingDialog.setTitle(message);
        mLoadingDialog.dismiss();
        mLoadingDialog.show();
    }

    protected void dismissDialog() {
        if (mLoadingDialog != null) {
            mLoadingDialog.dismiss();
        }
    }

    @Override
    public void onDestroy() {
        dismissDialog();
        super.onDestroy();
        if (mDialog != null) {
            mDialog.dismiss();
        }
    }
}